package jmine.tec.script.impl.beanshell.repository;

import static jmine.tec.script.impl.beanshell.BeanshellLanguageIdentifier.BEANSHELL;
import jmine.tec.script.impl.beanshell.BeanshellScriptDriver;
import jmine.tec.script.impl.beanshell.BeanshellScriptExecutor;
import jmine.tec.script.persistent.repository.BaseScriptLoader;
import jmine.tec.script.persistent.repository.ScriptLanguageBuilder;
import jmine.tec.script.persistent.repository.TransientScriptRepository;

/**
 * Classe utilitária para configurar um TransientScriptRepository com suporte adequado a execução de scripts Beanshell.
 * 
 * @author lundberg
 */
public final class BeanshellSupport {

    private static final String BSH_TYPE = "SCRIPT-BSH";

    /**
     * Construtor vazio
     */
    private BeanshellSupport() {
        // OK
    }

    /**
     * Adiciona suporte a execução de scripts Beanshell.
     * 
     * @param repository repository
     */
    public static void addTo(TransientScriptRepository repository) {
        addLanguage(repository);
        addLoader(repository);
        addExecutors(repository);
    }

    /**
     * @param repository repository
     */
    private static void addExecutors(TransientScriptRepository repository) {
        repository.addExecutor(new BeanshellScriptExecutor());
        repository.addExecutor(new BeanshellScriptExecutor());
        repository.addExecutor(new BeanshellScriptExecutor());
    }

    /**
     * @param repository repository
     */
    private static void addLoader(TransientScriptRepository repository) {
        repository.addLoader(new BaseScriptLoader(BSH_TYPE, new BeanshellScriptDriver()));
    }

    /**
     * @param repository repository
     */
    private static void addLanguage(TransientScriptRepository repository) {
        ScriptLanguageBuilder beanshell = ScriptLanguageBuilder.aLanguage();
        beanshell = beanshell.identifiedBy(BEANSHELL).comMnemonico("BEANSHELL");
        beanshell = beanshell.comAbreviacao("BSH").executadoPor(BeanshellScriptExecutor.class);
        repository.addLanguage(beanshell.build());
    }
}
